<?php
class PM_t
{
    public static function __index( $room_id , $user_id , $put_id , $card_number , $eat_card  , $db_connect ){
        //取出房间信息
        $room_info    = $db_connect->select('*')->from('tt_room')->where("room_id= '". $room_id ."' ")->row();
        //当前用户是否有吃牌的权限
        $user_index   = PHZ_Tools::__get_user_in_room_index( $room_info , $user_id );
        if( $room_info['p'. $user_index .'_is_chi'] != 1 ){
            return false;
        }
        //取出该房间当前这一局的手牌信息
        $card_info    = $db_connect->select('*')->from('tt_room_card')->where("jid = '". $room_info['room_js'] ."' and rid= '". $room_id ."' ")->orderByASC( array('id') )->query();
        //取出出牌者跟吃牌者分别的手牌信息
        $input_card_info = array();
        $eat_card_info   = array();
        foreach( $card_info as $key => $value ){
            if( $value['uid'] == $put_id ){$input_card_info = $value;}
            if( $value['uid'] == $user_id ){$eat_card_info = $value;}
        }
        //将出牌者的bp组，去除一个被吃的牌后，重新组合bp，更新到数据库中
        $input_bp = $input_card_info['bp'];
        $input_bp = explode(",",$input_bp);
        foreach( $input_bp as $key => $value ){
            if( $value == $card_number ){ unset( $input_bp[$key] );break; }
        }
        $db_connect->update('tt_room_card')->cols(array('bp'=> implode(",",$input_bp)  ))->where('id = '. $input_card_info['id'])->query();
        //吃牌者增加下牌
        $eat_xp = $eat_card_info['xp'] . $eat_card . ",";
        $db_connect->update('tt_room_card')->cols(array('xp'=> $eat_xp ))->where('id = '. $eat_card_info['id'])->query();
        //删除吃牌者手中的手牌
        $new_sp = PHZ_Tools::__chi_pai_update_sp( $eat_card_info , $card_number , $eat_card , $db_connect );
        $db_connect->update('tt_room_card')->cols(array('sp'=> $new_sp ))->where('id = '. $eat_card_info['id'])->query();
        //既然该牌被吃；那么就可以取消所有人的吃牌跟碰牌状态了（为什么可以取消碰牌，因为碰优先；碰的人不碰，吃的人是吃不了的）
        PHZ_Room::__clear_chi_AND_peng( $room_id , $db_connect );
        //吃牌人增加胡息数(这里算息，因为要考虑到可能有比牌的情况，那么这里需要先将切割，依次代入算息)
        $temp_eat_card = explode(",", $eat_card );
        if( count( $temp_eat_card ) >= 2 ){
            foreach ( $temp_eat_card as $key => $value ){
                PHZ_SuanXi::__xi($room_info, $user_id, "chi", $value, $db_connect);//比牌迭代算息
            }
        }else {
            PHZ_SuanXi::__xi($room_info, $user_id, "chi", $eat_card, $db_connect);//单吃算息
        }
        //用户是否有进字免打的权限？如果有；则直接下家摸牌操作
        $mianda = false;
        if( PHZ_Card::__get_User_TIPAO_Free_to_Play( $room_info , $user_id , $db_connect ) == true ){
            $mianda = true;
        }
        //对所有人广播吃牌确认信息
        $temp = array();
        $temp['pl']          = 9;//9为用户确认吃牌后的广播
        $temp['card_number'] = (int)$card_number;//被吃的这个牌的名称
        $temp['put_uid']     = $mianda == true ? 0 : $user_id;//出牌权归吃牌人所有(如果这里有进字免打的权限；则出牌权去除)
        $temp['mop_uid']     = 0;//所有人丧失摸牌权
        $temp['eat_card']    = $eat_card;//吃牌的组合，这个是用来给每个人展示使用的
        PHZ_Log::__send_user_log( $room_info , $user_id , $temp , $db_connect );
        //设置出牌权
        PHZ_Room::__setThisPutUser( $room_info['room_id'] , $temp['put_uid'] , $db_connect );
        //设置当前房间出牌人以及牌号 - 清空
        PHZ_Room::__setThisPutCard( $room_id , 0 , 0 , true , $db_connect );
        //如果免打，则吃碰畏牌的人下家自动摸牌
        if( $mianda == true ){
            PM_n::__index( $room_info , $user_id , 0 , $db_connect );
        }
        //吃牌人成功吃牌，等待吃牌人做操作
        return true;
    }
}